<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <title>The Node Beginner Book » A comprehensive Node.js tutorial</title>
        <meta name="description" content="A comprehensive Node.js tutorial for beginners: Learn how to build a full blown web application with server-side JavaScript" />
        
        <link rel="icon" href="favicon.png" type="image/png" />
        <link rel="stylesheet" type="text/css" href="default.css" />
    </head>
    <body>
        <div id="ibookstore-banner">
            <a href="buy-ibook/index.html"><img src="ibookstore-banner.jpg" width="100%" /></a>
        </div>

        <div id="translations">
            <table>
                <tr>
                    <td>
                        <a href="index-jp.html">
                            <div class="flag"><img src="jp-flag.png" width="24" height="24" alt="japanese flag" /></div>
                            <div class="text">日本語で読む</div>
                        </a>
                    </td>
                    <td>
                        <a href="index-es.html">
                            <div class="flag"><img src="es-flag.png" width="24" height="24" alt="spanish flag" /></div>
                            <div class="text">Lee este tutorial en Español</div>
                        </a>
                    </td>
                    <td>
                        <a href="index-kr.html">
                            <div class="flag"><img src="kr-flag.png" width="24" height="24" alt="korean flag" /></div>
                            <div class="text">이 튜토리얼을 한글로 보세요</div>
                        </a>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="index-zh-cn.html">
                            <div class="flag"><img src="cn-flag.png" width="24" height="24" alt="chinese flag" /></div>
                            <div class="text">阅读本书中文版</div>
                        </a>
                    </td>
                    <td>
                        <a href="index-zh-tw.html">
                            <div class="flag"><img src="cn-flag.png" width="24" height="24" alt="chinese flag" /></div>
                            <div class="text">阅读本书繁体中文版</div>
                        </a>
                    </td>
                    <td>
                        <a href="http://www.nodebeginner.ru">
                            <div class="flag"><img src="ru-flag.png" width="24" height="24" alt="russian flag" /></div>
                            <div class="text">Читать этот учебник на русском</div>
                        </a>
                    </td>
                </tr>
            </table>
        </div>

        <div class="buybox">
            <div class="buy-the-bundle">
                <div class="cover">
                    <p>
                        The perfect introduction plus the perfect reference in one bundle!
                    </p>
                    <a href="buy-bundle/index.html"><img src="the_node_beginner_book_cover_small.png" height="86" width="57" /></a>
                    <a href="buy-bundle/index.html"><img src="hands-on_node.js_cover.png" height="86" width="57" /></a>
                </div>
                <div class="description">
                    <p>
                        LeanBundle currently offers<br />
                        the final version of
                        <br />
                        <strong>The Node Beginner Book</strong>
                        <br />
                        plus Pedro Teixeira's excellent
                        <br />
                        <strong>Hands-on Node.js</strong> for only
                        <br />
                        <br />
                        <strong class="price dollarsign">$</strong><strong class="price">9.99</strong>
                        <br />
                        (regular price <del>$21.98</del>)
                    </p>
                </div>
                <div class="buy">
                    <p>
                        226 pages in total
                        <br />
                        PDF, ePub & MOBI
                        <br />
                        Direct download
                        <br />
                        Free updates
                    </p>
                    <a class="buttonlink" href="buy-bundle/index.html">
                        <div class="button">Buy this<br />bundle now</div>
                    </a>
                </div>
            </div>
        </div>


        <div id="book">
        <div>
        <h1>The Node Beginner Book</h1>

        <div id="author">A Node.js tutorial by <a href="http://twitter.com/manuelkiessling">Manuel Kiessling</a></div>

        <a name="about"></a>

        <h2>About</h2>

        <p>
            The aim of this document is to get you started with developing
            applications with Node.js, teaching you everything you need to
            know about "advanced" JavaScript along the way. It goes way
            beyond your typical "Hello World" tutorial.
        </p>

        <a name="status"></a>

        <h3>Status</h3>

        <p>
            You are reading the final version of this book, i.e., updates
            are only done to correct errors or to reflect changes in new
            versions of Node.js. It was last updated on July 1, 2013.
        </p>

        <p>
            The code samples in this book are tested to work with Node.js
            version 0.10.12.
        </p>

        <p>
            This site allows you to read pages 1-21 of this book for free.
            The complete text is available as a DRM-free eBook (PDF, ePub and
            Kindle format). More info is available at the end of the free part.
        </p>

        <a name="intended-audience"></a>

        <h3>Intended audience</h3>

        <p>
            This document will probably fit best for readers that have a
            background similar to my own: experienced with at least one
            object-oriented language like Ruby, Python, PHP or Java, only little
            experience with JavaScript, and completely new to Node.js.
        </p>

        <p>
            Aiming at developers that already have experience with other
            programming languages means that this document won't cover
            really basic stuff like data types, variables, control structures
            and the likes. You already need to know about these to understand
            this document.
        </p>

        <p>
            However, because functions and objects in JavaScript are different
            from their counterparts in most other languages, these will be
            explained in more detail.
        </p>

        <a name="structure"></a>

        <h3>Structure of this document</h3>

        <p>
            Upon finishing this document, you will have created a complete web
            application which allows the users of this application to view web
            pages and upload files.
        </p>

        <p>
            Which, of course, is not exactly
            world-changing, but we will go some extra miles and not only create
            the code that is "just enough" to make these use cases possible,
            but create a simple, yet complete framework to cleanly separate the
            different aspects of our application. You will see what I mean in a
            minute.
        </p>

        <p>
            We will start with looking at how JavaScript development in Node.js
            is different from JavaScript development in a browser.
        </p>

        <p>
            Next, we will stay with the good old tradition of writing a "Hello
            World" application, which is a most basic Node.js application that
            "does" something.
        </p>

        <p>
            Then, we will discuss what kind of "real" application we want to
            build, dissect the different parts which need to be implemented to
            assemble this application, and start working on each of these parts
            step-by-step.
        </p>

        <p>
            As promised, along the way we will learn about some of the more
            advanced concepts of JavaScript, how to make use of them, and
            look at why it makes sense to use these concepts instead of
            those we know from other programming languages.
        </p>

        <p>
            The source code of the finished application is available through
            <a href="https://github.com/ManuelKiessling/NodeBeginnerBook/tree/master/code/application">the
                NodeBeginnerBook Github repository</a>.
        </p>

        <div id="table-of-contents-headline">Table of contents</div>
        <div id="table-of-contents">
            <ul>

                <li><a href="#about">About</a>
                    <ul>
                        <li><a href="#status">Status</a></li>
                        <li><a href="#intended-audience">Intended audience</a></li>
                        <li><a href="#structure">Structure of this document</a></li>
                    </ul>
                </li>

                <li><a href="#javascript-and-nodejs">JavaScript and Node.js</a>
                    <ul>
                        <li><a href="#javascript-and-you">JavaScript and You</a></li>
                        <li><a href="#a-word-of-warning">A word of warning</a></li>
                        <li><a href="#server-side-javascript">Server-side JavaScript</a></li>
                        <li><a href="#hello-world">"Hello World"</a></li>

                    </ul>
                </li>
                <li><a href="#a-full-blown-web-application-with-nodejs">A full blown web application with Node.js</a>
                    <ul>
                        <li><a href="#the-use-cases">The use cases</a></li>
                        <li><a href="#the-application-stack">The application stack</a></li>
                    </ul>

                </li>
                <li><a href="#building-the-application-stack">Building the application stack</a>
                    <ul>
                        <li><a href="#a-basic-http-server">A basic HTTP server</a></li>
                        <li><a href="#analyzing-our-http-server">Analyzing our HTTP server</a></li>
                        <li><a href="#passing-functions-around">Passing functions around</a></li>
                        <li><a href="#how-function-passing-makes-our-http-server-work">How function passing makes our
                            HTTP server work</a></li>

                        <li><a href="#event-driven-callbacks">Event-driven asynchronous callbacks</a></li>
                        <li><a href="#how-our-server-handles-requests">How our server handles requests</a></li>
                        <li><a href="#finding-a-place-for-our-server-module">Finding a place for our server module</a>
                        </li>
                        <li><a href="#whats-needed-to-route-requests">What's needed to "route" requests?</a></li>
                        <li><a href="#execution-in-the-kongdom-of-verbs">Execution in the kingdom of verbs</a></li>
                        <li><a href="#routing-to-real-request-handlers">Routing to real request handlers</a></li>

                        <li><strong>Chapters available in the full book:</strong>
                            <li>Making the request handlers respond
                                <ul>
                                    <li>How to not do it</li>
                                    <li>Blocking and non-blocking</li>
                                    <li>Responding request handlers with non-blocking operation</li>
                                </ul>
                            </li>
                            <li>Serving something useful
                                <ul>
                                    <li>Handling POST requests</li>
                                    <li>Handling file uploads</li>
                                </ul>
                            </li>
                            <li>Conclusion and outlook</li>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>

        <a name="javascript-and-nodejs"></a>

        <h2>JavaScript and Node.js</h2>

        <a name="javascript-and-you"></a>

        <h3>JavaScript and You</h3>

        <p>
            Before we talk about all the technical stuff, let's take a
            moment and talk about you and your relationship with
            JavaScript. This chapter is here to allow you to estimate
            if reading this document any further makes sense for you.
        </p>

        <p>
            If you are like me, you started with HTML "development"
            long ago, by writing HTML documents. You came along this
            funny thing called JavaScript, but you only used it in a
            very basic way, adding interactivity to your web pages
            every now and then.
        </p>

        <p>
            What you really wanted was "the real thing", you wanted to
            know how to build complex web sites - you learned a
            programming language like PHP, Ruby, Java, and started
            writing "backend" code.
        </p>

        <p>
            Nevertheless, you kept an eye on JavaScript, you saw that
            with the introduction of jQuery, Prototype and the likes,
            things got more advanced in JavaScript land, and that this
            language really was about more than <em>window.open()</em>.
        </p>

        <p>
            However, this was all still frontend stuff, and although it
            was nice to have jQuery at your disposal whenever you felt
            like spicing up a web page, at the end of the day you were,
            at best, a JavaScript <em>user</em>, but not a JavaScript
            <em>developer</em>.
        </p>

        <p>
            And then came Node.js. JavaScript on the server, how cool
            is that?
        </p>

        <p>
            You decided that it's about time to check out the old, new
            JavaScript. But wait, writing Node.js applications is the
            one thing; understanding why they need to be written the
            way they are written means - understanding JavaScript.
            And this time for real.
        </p>

        <p>
            Here is the problem: Because JavaScript really lives two,
            maybe even three lives (the funny little DHTML helper from
            the mid-90's, the more serious frontend stuff like jQuery
            and the likes, and now server-side), it's not that easy to
            find information that helps you to learn JavaScript the
            "right" way, in order to write Node.js applications in a
            fashion that makes you feel you are not just using
            JavaScript, you are actually developing it.
        </p>

        <p>
            Because that's the catch: you already are an experienced
            developer, you don't want to learn a new technique by just
            hacking around and mis-using it; you want to be sure that
            you are approaching it from the right angle.
        </p>

        <p>
            There is, of course, excellent documentation out there.
            But documentation alone sometimes isn't enough. What is
            needed is guidance.
        </p>

        <p>
            My goal is to provide a guide for you.
        </p>

        <a name="a-word-of-warning"></a>

        <h3>A word of warning</h3>

        <p>
            There are some really excellent JavaScript people out
            there. I'm not one of them.
        </p>

        <p>
            I'm really just the guy I talked about in the previous
            paragraph. I know a thing or two about developing backend
            web applications, but I'm still new to "real" JavaScript
            and still new to Node.js. I learned some of the more
            advanced aspects of JavaScript just recently.
            I'm not experienced.
        </p>

        <p>
            Which is why this is no "from novice to expert" book. It's
            more like "from novice to advanced novice".
        </p>

        <p>
            If I don't fail, then this will be the kind of
            document I wish I had when starting with Node.js.
        </p>

        <a name="server-side-javascript"></a>

        <h3>Server-side JavaScript</h3>

        <p>
            The first incarnations of JavaScript lived in browsers.
            But this is just the context. It defines what you can
            do with the language, but it doesn't say much about what
            the language itself can do. JavaScript is a "complete"
            language: you can use it in many contexts and achieve
            everything with it you can achieve with any other
            "complete" language.
        </p>

        <p>
            Node.js really is just another context: it allows you to run
            JavaScript code in the backend, outside a browser.
        </p>

        <p>
            In order to execute the JavaScript you intend to run in the
            backend, it needs to be interpreted and, well, executed.
            This is what Node.js does, by making use of Google's V8 VM, the
            same runtime environment for JavaScript that Google
            Chrome uses.
        </p>

        <p>
            Plus, Node.js ships with a lot of useful modules, so you don't
            have to write everything from scratch, like for example
            something that outputs a string on the console.
        </p>

        <p>
            Thus, Node.js is really two things: a runtime environment and a
            library.
        </p>

        <p>
            In order to make use of these, you need to install Node.js.
            Instead of repeating the process here, I kindly ask you to
            visit
            <a href="https://github.com/joyent/node/wiki/Installation" title="Building and Installing Node.js">the
                official
                installation instructions</a>. Please come back once you
            are up and running.
        </p>

        <a name="hello-world"></a>

        <h3>"Hello World"</h3>

        <p>
            Ok, let's just jump in the cold water and write our first
            Node.js application: "Hello World".
        </p>

        <p>
            Open your favorite editor and create a file called
            <em>helloworld.js</em>. We want it to write "Hello World"
            to STDOUT, and here is the code needed to do that:
        </p>
        <pre class="prettyprint lang-js"><span class="pln">console</span><span class="pun">.</span><span
                class="pln">log</span><span class="pun">(</span><span class="str">"Hello World"</span><span class="pun">);</span></pre>
        <p>
            Save the file, and execute it through Node.js:
        </p>

        <pre>node helloworld.js</pre>
        <p>
            This should output <em>Hello World</em> on your terminal.
        </p>

        <p>
            Ok, this stuff is boring, right? Let's write some real
            stuff.
        </p>


        <a name="a-full-blown-web-application-with-nodejs"></a>

        <h2>A full blown web application with Node.js</h2>

        <a name="the-use-cases"></a>

        <h3>The use cases</h3>

        <p>
            Let's keep it simple, but realistic:
        </p>
        <ul>

            <li>
                The user should be able to use our application with
                a web browser
            </li>
            <li>
                The user should see a welcome page when
                requesting http://<em>domain</em>/start which displays a
                file upload form
            </li>
            <li>
                By choosing an image file to upload and submitting the
                form, this image should then be uploaded to
                http://<em>domain</em>/upload, where it is displayed once
                the upload is finished
            </li>

        </ul>
        <p>
            Fair enough. Now, you could achieve this goal by googling
            and hacking together <em>something</em>. But that's not
            what we want to do here.
        </p>

        <p>
            Furthermore, we don't want to write only the most basic
            code to achieve the goal, however elegant and correct this code
            might be. We will intentionally add more abstraction than
            necessary in order to get a feeling for building more
            complex Node.js applications.
        </p>

        <a name="the-application-stack"></a>

        <h3>The application stack</h3>

        <p>
            Let's dissect our application. Which parts need to be
            implemented in order to fulfill the use cases?
        </p>

        <ul>
            <li>
                We want to serve web pages, therefore we need an
                <strong>HTTP server</strong>
            </li>

            <li>
                Our server will need to answer differently to
                requests, depending on which URL the request was
                asking for, thus we need some kind of
                <strong>router</strong> in order to map requests
                to request handlers
            </li>
            <li>
                To fullfill the requests that arrived at the server
                and have been routed using the router, we need
                actual <strong>request handlers</strong>
            </li>
            <li>

                The router probably should also treat any incoming
                POST data and give it to the request handlers in
                a convenient form, thus we need <strong>request
                data handling</strong>
            </li>
            <li>
                We not only want to handle requests for URLs, we
                also want to display content when these URLs are
                requested, which means we need some kind of
                <strong>view logic</strong> the request handlers
                can use in order to send content to the user's
                browser
            </li>
            <li>
                Last but not least, the user will be able to upload
                images, so we are going to need some kind of
                <strong>upload handling</strong> which takes care of
                the details
            </li>
        </ul>

        <p>
            Let's think a moment about how we would build this stack
            with PHP. It's not exactly a secret that the typical setup
            would be an Apache HTTP server with mod_php5 installed.
            <br>
            Which in turn means that the whole "we need to be able to
            serve web pages and receive HTTP requests" stuff doesn't
            happen within PHP itself.
        </p>

        <p>
            Well, with node, things are a bit different. Because with
            Node.js, we not only implement our application, we also
            implement the whole HTTP server. In fact, our web
            application and its web server are basically the same.
        </p>

        <p>
            This might sound like a lot of work, but we will see in a
            moment that with Node.js, it's not.
        </p>

        <p>
            Let's just start at the beginning and implement the first
            part of our stack, the HTTP server.
        </p>

        <a name="building-the-application-stack"></a>

        <h2>Building the application stack</h2>

        <a name="a-basic-http-server"></a>

        <h3>A basic HTTP server</h3>

        <p>
            When I arrived at the point where I wanted to start with my
            first "real" Node.js application, I wondered not only how to
            actually code it, but also how to organize my code.
            <br>
            Do I need to have everything in one file? Most tutorials on
            the web that teach you how to write a basic HTTP server in
            Node.js have all the logic in one place. What if I want to
            make sure that my code stays readable the more stuff I
            implement?
        </p>

        <p>
            Turns out, it's relatively easy to keep the different
            concerns of your code separated, by putting them in
            modules.
        </p>

        <p>
            This allows you to have a clean main file, which you
            execute with Node.js, and clean modules that can be used by
            the main file and among each other.
        </p>

        <p>
            So, let's create a main file which we use to start our
            application, and a module file where our HTTP server code
            lives.
        </p>

        <p>
            My impression is that it's more or less a standard to name
            your main file <em>index.js</em>. It makes sense to put our
            server module into a file named <em>server.js</em>.
        </p>

        <p>
            Let's start with the server module. Create the file
            <em>server.js</em> in the root directory of your project,
            and fill it with the following code:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br>http</span><span
                class="pun">.</span><span class="pln">createServer</span><span class="pun">(</span><span class="kwd">function</span><span
                class="pun">(</span><span class="pln">request</span><span class="pun">,</span><span class="pln"> response</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; response</span><span
                class="pun">.</span><span class="pln">writeHead</span><span class="pun">(</span><span
                class="lit">200</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">"Content-Type"</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">"text/plain"</span><span class="pun">});</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">write</span><span
                class="pun">(</span><span class="str">"Hello World"</span><span class="pun">);</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">end</span><span
                class="pun">();</span><span class="pln"><br></span><span class="pun">}).</span><span
                class="pln">listen</span><span class="pun">(</span><span class="lit">8888</span><span
                class="pun">);</span></pre>

        <p>
            That's it! You just wrote a working HTTP server. Let's
            prove it by running and testing it. First, execute your
            script with Node.js:
        </p>
        <pre>node server.js</pre>
        <p>
            Now, open your browser and point it at
            <a href="http://localhost:8888/" rel="nofollow">http://localhost:8888/</a>.
            This should display a web page that says "Hello World".
        </p>

        <p>
            That's quite interesting, isn't it. How about talking about
            what's going on here and leaving the question of how to
            organize our project for later? I promise we'll get back to
            it.
        </p>

        <a name="analyzing-our-http-server"></a>

        <h3>Analyzing our HTTP server</h3>

        <p>
            Well, then, let's analyze what's actually going on here.
        </p>

        <p>
            The first line <em>require</em>s the <em>http</em> module
            that ships with Node.js and makes it accessible through the
            variable <em>http</em>.
        </p>

        <p>
            We then call one of the functions the http module offers:
            <em>createServer</em>. This function returns an object, and
            this object has a method named <em>listen</em>, and takes
            a numeric value which indicates the port number our HTTP
            server is going to listen on.
        </p>

        <p>
            Please ignore for a second the function definition that
            follows the opening bracket of <em>http.createServer</em>.
        </p>

        <p>
            We could have written the code that starts our server and
            makes it listen at port 8888 like this:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="kwd">var</span><span
                class="pln"> server </span><span class="pun">=</span><span class="pln"> http</span><span
                class="pun">.</span><span class="pln">createServer</span><span class="pun">();</span><span
                class="pln"><br>server</span><span class="pun">.</span><span class="pln">listen</span><span class="pun">(</span><span
                class="lit">8888</span><span class="pun">);</span></pre>

        <p>
            That would start an HTTP server listening at port 8888
            and doing nothing else (not even answering any incoming
            requests).
        </p>

        <p>
            The really interesting (and, if your background is a more
            conservative language like PHP, odd looking) part is the
            function definition right there where you would expect the
            first parameter of the <em>createServer()</em> call.
        </p>

        <p>
            Turns out, this function definition IS the first (and only)
            parameter we are giving to the <em>createServer()</em>

            call. Because in JavaScript, functions can be passed around
            like any other value.
        </p>

        <a name="passing-functions-around"></a>

        <h3>Passing functions around</h3>

        <p>
            You can, for example, do something like this:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">function</span><span class="pln"> say</span><span
                class="pun">(</span><span class="pln">word</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span
                class="pln">word</span><span class="pun">);</span><span class="pln"><br></span><span
                class="pun">}</span><span class="pln"><br><br></span><span class="kwd">function</span><span class="pln"> execute</span><span
                class="pun">(</span><span class="pln">someFunction</span><span class="pun">,</span><span class="pln"> value</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; someFunction</span><span
                class="pun">(</span><span class="pln">value</span><span class="pun">);</span><span
                class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>execute</span><span
                class="pun">(</span><span class="pln">say</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">"Hello"</span><span class="pun">);</span></pre>

        <p>
            Read this carefully! What we are doing here is, we pass the
            function <em>say</em> as the first parameter to the
            <em>execute</em> function. Not the return value of
            <em>say</em>, but <em>say</em> itself!
        </p>

        <p>
            Thus, <em>say</em> becomes the local variable
            <em>someFunction</em> within <em>execute</em>, and execute
            can call the function in this variable by issuing
            <em>someFunction()</em> (adding brackets).
        </p>

        <p>
            Of course, because <em>say</em> takes one parameter,
            <em>execute</em> can pass such a parameter when calling
            <em>someFunction</em>.
        </p>

        <p>
            We can, as we just did, pass a function as a parameter to
            another function by its name. But we don't have to take this
            indirection of first defining, then passing it - we can
            define and pass a function as a parameter to another
            function in-place:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">function</span><span class="pln"> execute</span><span
                class="pun">(</span><span class="pln">someFunction</span><span class="pun">,</span><span class="pln"> value</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; someFunction</span><span
                class="pun">(</span><span class="pln">value</span><span class="pun">);</span><span
                class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>execute</span><span
                class="pun">(</span><span class="kwd">function</span><span class="pun">(</span><span
                class="pln">word</span><span class="pun">){</span><span class="pln"> console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span
                class="pln">word</span><span class="pun">)</span><span class="pln"> </span><span
                class="pun">},</span><span class="pln"> </span><span class="str">"Hello"</span><span
                class="pun">);</span></pre>

        <p>
            We define the function we want to pass to <em>execute</em>
            right there at the place where <em>execute</em> expects its
            first parameter.
        </p>

        <p>
            This way, we don't even need to give the function a name,
            which is why this is called an <em>anonymous function</em>.
        </p>

        <p>
            This is a first glimpse at what I like to call "advanced"
            JavaScript, but let's take it step by step. For now, let's
            just accept that in JavaScript, we can pass a function as
            a parameter when calling another function. We can do this
            by assigning our function to a variable, which we then
            pass, or by defining the function to pass in-place.
        </p>

        <a name="how-function-passing-makes-our-http-server-work"></a>

        <h3>How function passing makes our HTTP server work</h3>

        <p>
            With this knowledge, let's get back to our minimalistic
            HTTP server:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br>http</span><span
                class="pun">.</span><span class="pln">createServer</span><span class="pun">(</span><span class="kwd">function</span><span
                class="pun">(</span><span class="pln">request</span><span class="pun">,</span><span class="pln"> response</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; response</span><span
                class="pun">.</span><span class="pln">writeHead</span><span class="pun">(</span><span
                class="lit">200</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">"Content-Type"</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">"text/plain"</span><span class="pun">});</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">write</span><span
                class="pun">(</span><span class="str">"Hello World"</span><span class="pun">);</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">end</span><span
                class="pun">();</span><span class="pln"><br></span><span class="pun">}).</span><span
                class="pln">listen</span><span class="pun">(</span><span class="lit">8888</span><span
                class="pun">);</span></pre>

        <p>
            By now it should be clear what we are actually doing here:
            we pass the <em>createServer</em> function an anonymous
            function.
        </p>

        <p>
            We could achieve the same by refactoring our code to:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="kwd">function</span><span
                class="pln"> onRequest</span><span class="pun">(</span><span class="pln">request</span><span
                class="pun">,</span><span class="pln"> response</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; response</span><span
                class="pun">.</span><span class="pln">writeHead</span><span class="pun">(</span><span
                class="lit">200</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">"Content-Type"</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">"text/plain"</span><span class="pun">});</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">write</span><span
                class="pun">(</span><span class="str">"Hello World"</span><span class="pun">);</span><span
                class="pln"><br>&nbsp; response</span><span class="pun">.</span><span class="pln">end</span><span
                class="pun">();</span><span class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>http</span><span
                class="pun">.</span><span class="pln">createServer</span><span class="pun">(</span><span class="pln">onRequest</span><span
                class="pun">).</span><span class="pln">listen</span><span class="pun">(</span><span
                class="lit">8888</span><span class="pun">);</span></pre>

        <p>
            Maybe now is a good moment to ask: Why are we doing it
            that way?
        </p>

        <a name="event-driven-callbacks"></a>

        <h3>Event-driven asynchronous callbacks</h3>

        <p>
            To understand why Node.js applications have to be written this way,
            we need to understand how Node.js executes our code. Node's
            approach isn't unique, but the underlying execution model is
            different from runtime environments like Python, Ruby, PHP or Java.
        </p>

        <p>
            Let's take a very simple piece of code like this:
        </p>

        <pre class="prettyprint lang-js">var result = database.query("SELECT * FROM hugetable");
console.log("Hello World");</pre>

        <p>
            Please ignore for now that we haven't actually talked about
            connecting to databases before - it's just an example. The
            first line queries a database for lots of rows, the second
            line puts "Hello World" to the console.
        </p>

        <p>
            Let's assume that the database query is really slow, that it has
            to read an awful lot of rows, which takes several seconds.
        </p>

        <p>
            The way we have written this code, the JavaScript interpreter of
            Node.js first has to read the complete result set from the
            database, and then it can execute the <em>console.log()</em>
            function.
        </p>

        <p>
            If this piece of code actually was, say, PHP, it would work the
            same way: read all the results at once, then execute the next line
            of code. If this code would be part of a web page script, the user
            would have to wait several seconds for the page to load.
        </p>

        <p>
            However, in the execution model of PHP, this would not become a
            "global" problem: the web server starts its own PHP process for
            every HTTP request it receives. If one of these requests results
            in the execution of a slow piece of code, it results in a slow
            page load for this particular user, but other users requesting
            other pages would not be affected.
        </p>

        <p>
            The execution model of Node.js is different - there is only one
            single process. If there is a slow database query somewhere in
            this process, this affects the whole process - everything comes
            to a halt until the slow query has finished.
        </p>

        <p>
            To avoid this, JavaScript, and therefore Node.js, introduces the
            concept of event-driven, asynchronous callbacks, by utilizing an
            event loop.
        </p>

        <p>
            We can understand this concept by analyzing a rewritten version
            of our problematic code:
        </p>

        <pre class="prettyprint lang-js">database.query("SELECT * FROM hugetable", function(rows) {
  var result = rows;
});
console.log("Hello World");</pre>

        <p>
            Here, instead of expecting <em>database.query()</em> to directly
            return a result to us, we pass it a second parameter, an anonymous
            function.
        </p>

        <p>
            In its previous form, our code was synchronous: <em>first</em>
            do the database query, and only when this is done, <em>then</em>
            write to the console.
        </p>

        <p>
            Now, Node.js can handle the database request asynchronously.
            Provided that <em>database.query()</em> is part of an asynchronous
            library, this is what Node.js does: just as before, it takes the
            query and sends it to the database. But instead of waiting for it
            to be finished, it makes a mental note that says "When at some
            point in the future the database server is done and sends the
            result of the query, then I have to execute the anonymous function
            that was passed to <em>database.query()</em>."
        </p>

        <p>
            Then, it immediately executes <em>console.log()</em>, and
            afterwards, it enters the event loop. Node.js continuously cycles
            through this loop again and again whenever there is nothing else
            to do, waiting for events. Events like, e.g., a slow database
            query finally delivering its results.
        </p>

        <p>
            This also explains why our HTTP server needs a function it can
            call upon incoming requests - if Node.js would start the server
            and then just pause, waiting for the next request, continuing
            only when it arrives, that would be highly inefficent. If a second
            user requests the server while it is still serving the first
            request, that second request could only be answered after the first
            one is done - as soon as you have more than a handful of HTTP
            requests per second, this wouldn't work at all.
        </p>

        <p>
            It's important to note that this asynchronous, single-threaded,
            event-driven execution model isn't an infinitely scalable
            performance unicorn with silver bullets attached. It is just one
            of several models, and it has its limitations, one being that as
            of now, Node.js is just one single process, and it can run on only
            one single CPU core. Personally, I find this model quite
            approachable, because it allows to write applications that have to
            deal with concurrency in an efficient and relatively
            straightforward manner.
        </p>

        <p>
            You might want to take the time to read Felix
            Geisendörfer's excellent post
            <a href="http://debuggable.com/posts/understanding-node-js:4bd98440-45e4-4a9a-8ef7-0f7ecbdd56cb">Understanding
                node.js</a>
            for additional background explanation.
        </p>

        <p>
            Let's play around a bit with this new concept. Can we prove
            that our code continues after creating the server, even if
            no HTTP request happened and the callback function we
            passed isn't called? Let's try it:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="kwd">function</span><span
                class="pln"> onRequest</span><span class="pun">(</span><span class="pln">request</span><span
                class="pun">,</span><span class="pln"> response</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Request received."</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; response</span><span class="pun">.</span><span
                class="pln">writeHead</span><span class="pun">(</span><span class="lit">200</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"Content-Type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text/plain"</span><span
                class="pun">});</span><span class="pln"><br>&nbsp; response</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="str">"Hello World"</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; response</span><span class="pun">.</span><span
                class="pln">end</span><span class="pun">();</span><span class="pln"><br></span><span
                class="pun">}</span><span class="pln"><br><br>http</span><span class="pun">.</span><span class="pln">createServer</span><span
                class="pun">(</span><span class="pln">onRequest</span><span class="pun">).</span><span class="pln">listen</span><span
                class="pun">(</span><span class="lit">8888</span><span class="pun">);</span><span class="pln"><br><br>console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Server has started."</span><span
                class="pun">);</span></pre>

        <p>
            Note that I use <em>console.log</em> to output a text whenever
            the <em>onRequest</em> function (our callback) is triggered,
            and another text right <em>after</em> starting the HTTP server.
        </p>

        <p>
            When we start this (<em>node server.js</em>, as always), it
            will immediately output "Server has started." on the
            command line. Whenever we request our server (by opening
            <a href="http://localhost:8888/" rel="nofollow">http://localhost:8888/</a>

            in our browser), the message "Request received." is printed
            on the command line.
        </p>

        <p>
            Event-driven asynchronous server-side JavaScript with
            callbacks in action :-)
        </p>

        <p>
            (Note that our server will probably write "Request received."
            to STDOUT two times upon opening the page in a browser. That's
            because most browsers will try to load the favicon by requesting
            http://localhost:8888/favicon.ico whenever you open
            http://localhost:8888/).
        </p>

        <a name="how-our-server-handles-requests"></a>

        <h3>How our server handles requests</h3>

        <p>
            Ok, let's quickly analyze the rest of our server code, that
            is, the body of our callback function <em>onRequest()</em>.
        </p>

        <p>
            When the callback fires and our <em>onRequest()</em> function
            gets triggered, two parameters are passed into it:
            <em>request</em> and <em>response</em>.
        </p>

        <p>
            Those are objects, and you can use their methods to handle
            the details of the HTTP request that occured and to respond
            to the request (i.e., to actually send something over
            the wire back to the browser that requested your server).
        </p>

        <p>
            And our code does just that: Whenever a request is
            received, it uses the <em>response.writeHead()</em>
            function to send an HTTP status 200 and content-type in the
            HTTP response header, and the <em>response.write()</em>
            function to send the text "Hello World" in the HTTP
            response body.
        </p>

        <p>
            At last, we call <em>response.end()</em> to actually finish
            our response.
        </p>

        <p>
            At this point, we don't care for the details of the
            request, which is why we don't use the <em>request</em>
            object at all.
        </p>

        <a name="finding-a-place-for-our-server-module"></a>

        <h3>Finding a place for our server module</h3>

        <p>
            Ok, I promised we will get back to how to organize our
            application. We have the code for a very basic HTTP server in
            the file <em>server.js</em>, and I mentioned that it's common
            to have a main file called <em>index.js</em> which is used
            to bootstrap and start our application by making use of the
            other modules of the application (like the HTTP server module
            that lives in <em>server.js</em>).
        </p>

        <p>
            Let's talk about how to make server.js a real Node.js module
            that can be used by our yet-to-be-written <em>index.js</em>
            main file.
        </p>

        <p>
            As you may have noticed, we already used modules in our code,
            like this:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="pun">...</span><span
                class="pln"><br><br>http</span><span class="pun">.</span><span class="pln">createServer</span><span
                class="pun">(...);</span></pre>

        <p>
            Somewhere within Node.js lives a module called "http", and we can
            make use of it in our own code by requiring it and assigning
            the result of the require to a local variable.
        </p>

        <p>
            This makes our local variable an object that carries all the
            public methods the <em>http</em> module provides.
        </p>

        <p>
            It's common practice to choose the name of the module for the
            name of the local variable, but we are free to choose whatever
            we like:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> foo </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="pun">...</span><span
                class="pln"><br><br>foo</span><span class="pun">.</span><span class="pln">createServer</span><span
                class="pun">(...);</span></pre>

        <p>
            Fine, it's clear how to make use of internal Node.js modules. How
            do we create our own modules, and how do we use them?
        </p>

        <p>
            Let's find out by turning our <em>server.js</em> script into a
            real module.
        </p>

        <p>
            Turns out, we don't have to change that much. Making some code
            a module means we need to <em>export</em> those parts of its
            functionality that we want to provide to scripts that require
            our module.
        </p>

        <p>
            For now, the functionality our HTTP server needs to export is
            simple: scripts requiring our server module simply need to
            start the server.
        </p>

        <p>
            To make this possible, we will put our server code into a
            function named <em>start</em>, and we will export this
            function:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br><br></span><span class="kwd">function</span><span
                class="pln"> start</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span
                class="pln"><br>&nbsp; </span><span class="kwd">function</span><span class="pln"> onRequest</span><span
                class="pun">(</span><span class="pln">request</span><span class="pun">,</span><span class="pln"> response</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; &nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Request received."</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">writeHead</span><span class="pun">(</span><span class="lit">200</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"Content-Type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text/plain"</span><span
                class="pun">});</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="str">"Hello World"</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">end</span><span class="pun">();</span><span class="pln"><br>&nbsp; </span><span class="pun">}</span><span
                class="pln"><br><br>&nbsp; http</span><span class="pun">.</span><span
                class="pln">createServer</span><span class="pun">(</span><span class="pln">onRequest</span><span
                class="pun">).</span><span class="pln">listen</span><span class="pun">(</span><span
                class="lit">8888</span><span class="pun">);</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Server has started."</span><span
                class="pun">);</span><span class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>exports</span><span
                class="pun">.</span><span class="pln">start </span><span class="pun">=</span><span
                class="pln"> start</span><span class="pun">;</span></pre>

        <p>
            This way, we can now create our main file <em>index.js</em>,
            and start our HTTP there, although the code for the server is
            still in our <em>server.js</em> file.
        </p>

        <p>
            Create a file <em>index.js</em> with the following content:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> server </span><span class="pun">=</span><span
                class="pln"> require</span><span class="pun">(</span><span class="str">"./server"</span><span
                class="pun">);</span><span class="pln"><br><br>server</span><span class="pun">.</span><span class="pln">start</span><span
                class="pun">();</span></pre>

        <p>
            As you can see, we can use our server module just like any
            internal module: by requiring its file and assigning it to
            a variable, its exported functions become available to us.
        </p>

        <p>
            That's it. We can now start our app via our main script, and it
            still does exactly the same:
        </p>
        <pre>node index.js</pre>

        <p>
            Great, we now can put the different parts of our application
            into different files and wire them together by making them
            modules.
        </p>

        <p>
            We still have only the very first part of our application in
            place: we can receive HTTP requests. But we need to do
            something with them - depending on which URL the browser
            requested from our server, we need to react differently.
        </p>

        <p>
            For a very simple application, you could do this directly
            within the callback function <em>onRequest()</em>. But as I said,
            let's add a bit more abstraction in order to make our example
            application a bit more interesting.
        </p>

        <p>
            Making different HTTP requests point at different parts of our
            code is called "routing" - well, then, let's create a module
            called <em>router</em>.
        </p>

        <a name="whats-needed-to-route-requests"></a>

        <h3>What's needed to "route" requests?</h3>

        <p>
            We need to be able to feed the requested URL and possible
            additional GET and POST parameters into our router, and based
            on these the router then needs to be able to decide which code
            to execute (this "code to execute" is the third part of our
            application: a collection of request handlers that do the
            actual work when a request is received).
        </p>

        <p>
            So, we need to look into the HTTP requests and extract the
            requested URL as well as the GET/POST parameters from them.
            It could be argued if that should be part of the router or
            part of the server (or even a module of its own), but let's
            just agree on making it part of our HTTP server for now.
        </p>

        <p>
            All the information we need is available through the
            <em>request</em> object which is passed as the first parameter
            to our callback function <em>onRequest()</em>. But to interpret
            this information, we need some additional Node.js modules, namely
            <em>url</em> and <em>querystring</em>.
        </p>

        <a name="head20"></a>
        <p>
            The <em>url</em> module provides methods which allow us to
            extract the different parts of a URL (like e.g. the requested
            path and query string), and <em>querystring</em> can in turn be
            used to parse the query string for request parameters:
        </p>
<pre>                               url.parse(string).query
                                           |
           url.parse(string).pathname      |
                       |                   |
                       |                   |
                     ------ -------------------
http://localhost:8888/start?foo=bar&amp;hello=world
                                ---       -----
                                 |          |
                                 |          |
              querystring(string)["foo"]    |
                                            |
                         querystring(string)["hello"]
</pre>

        <p>
            We can, of course, also use <em>querystring</em> to parse the
            body of a POST request for parameters, as we will see
            later.
        </p>

        <p>
            Let's now add to our <em>onRequest()</em> function the logic
            needed to find out which URL path the browser requested:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br></span><span
                class="kwd">var</span><span class="pln"> url </span><span class="pun">=</span><span
                class="pln"> require</span><span class="pun">(</span><span class="str">"url"</span><span
                class="pun">);</span><span class="pln"><br><br></span><span class="kwd">function</span><span
                class="pln"> start</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span
                class="pln"><br>&nbsp; </span><span class="kwd">function</span><span class="pln"> onRequest</span><span
                class="pun">(</span><span class="pln">request</span><span class="pun">,</span><span class="pln"> response</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; &nbsp; </span><span
                class="kwd">var</span><span class="pln"> pathname </span><span class="pun">=</span><span class="pln"> url</span><span
                class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span
                class="pln">request</span><span class="pun">.</span><span class="pln">url</span><span
                class="pun">).</span><span class="pln">pathname</span><span class="pun">;</span><span class="pln"><br>&nbsp; &nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Request for "</span><span
                class="pln"> </span><span class="pun">+</span><span class="pln"> pathname </span><span
                class="pun">+</span><span class="pln"> </span><span class="str">" received."</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">writeHead</span><span class="pun">(</span><span class="lit">200</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"Content-Type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text/plain"</span><span
                class="pun">});</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="str">"Hello World"</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">end</span><span class="pun">();</span><span class="pln"><br>&nbsp; </span><span class="pun">}</span><span
                class="pln"><br><br>&nbsp; http</span><span class="pun">.</span><span
                class="pln">createServer</span><span class="pun">(</span><span class="pln">onRequest</span><span
                class="pun">).</span><span class="pln">listen</span><span class="pun">(</span><span
                class="lit">8888</span><span class="pun">);</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Server has started."</span><span
                class="pun">);</span><span class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>exports</span><span
                class="pun">.</span><span class="pln">start </span><span class="pun">=</span><span
                class="pln"> start</span><span class="pun">;</span></pre>

        <p>
            Fine. Our application can now distinguish requests based on the
            URL path requested - this allows us to map requests to our
            request handlers based on the URL path using our (yet to be
            written) router.
        </p>

        <p>
            In the context of our application, it simply means that we will
            be able to have requests for the <em>/start</em> and
            <em>/upload</em> URLs handled by different parts of our
            code. We will see how everything fits together soon.
        </p>

        <p>
            Ok, it's time to actually write our router. Create a new file
            called <em>router.js</em>, with the following content:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">function</span><span class="pln"> route</span><span
                class="pun">(</span><span class="pln">pathname</span><span class="pun">)</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"About to route a request for "</span><span
                class="pln"> </span><span class="pun">+</span><span class="pln"> pathname</span><span
                class="pun">);</span><span class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>exports</span><span
                class="pun">.</span><span class="pln">route </span><span class="pun">=</span><span
                class="pln"> route</span><span class="pun">;</span></pre>

        <p>
            Of course, this code basically does nothing, but that's ok for
            now. Let's first see how to wire together this router with our
            server before putting more logic into the router.
        </p>

        <p>
            Our HTTP server needs to know about and make use of our router.
            We could hard-wire this dependency into the server, but because
            we learned the hard way from our experience with other
            programming languages, we are going to loosely couple server
            and router by injecting this dependency (you may want to read
            <a href="http://martinfowler.com/articles/injection.html">Martin Fowlers excellent post on Dependency Injection</a>
            for background information).
        </p>

        <p>
            Let's first extend our server's <em>start()</em> function in
            order to enable us to pass the route function to be used by
            parameter:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> http </span><span
                class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"http"</span><span class="pun">);</span><span class="pln"><br></span><span
                class="kwd">var</span><span class="pln"> url </span><span class="pun">=</span><span
                class="pln"> require</span><span class="pun">(</span><span class="str">"url"</span><span
                class="pun">);</span><span class="pln"><br><br></span><span class="kwd">function</span><span
                class="pln"> start</span><span class="pun">(</span><span class="pln">route</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span
                class="pln"><br>&nbsp; </span><span class="kwd">function</span><span class="pln"> onRequest</span><span
                class="pun">(</span><span class="pln">request</span><span class="pun">,</span><span class="pln"> response</span><span
                class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br>&nbsp; &nbsp; </span><span
                class="kwd">var</span><span class="pln"> pathname </span><span class="pun">=</span><span class="pln"> url</span><span
                class="pun">.</span><span class="pln">parse</span><span class="pun">(</span><span
                class="pln">request</span><span class="pun">.</span><span class="pln">url</span><span
                class="pun">).</span><span class="pln">pathname</span><span class="pun">;</span><span class="pln"><br>&nbsp; &nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Request for "</span><span
                class="pln"> </span><span class="pun">+</span><span class="pln"> pathname </span><span
                class="pun">+</span><span class="pln"> </span><span class="str">" received."</span><span
                class="pun">);</span><span class="pln"><br><br>&nbsp; &nbsp; route</span><span class="pun">(</span><span
                class="pln">pathname</span><span class="pun">);</span><span
                class="pln"><br><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">writeHead</span><span class="pun">(</span><span class="lit">200</span><span
                class="pun">,</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">"Content-Type"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"text/plain"</span><span
                class="pun">});</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">write</span><span class="pun">(</span><span class="str">"Hello World"</span><span
                class="pun">);</span><span class="pln"><br>&nbsp; &nbsp; response</span><span class="pun">.</span><span
                class="pln">end</span><span class="pun">();</span><span class="pln"><br>&nbsp; </span><span class="pun">}</span><span
                class="pln"><br><br>&nbsp; http</span><span class="pun">.</span><span
                class="pln">createServer</span><span class="pun">(</span><span class="pln">onRequest</span><span
                class="pun">).</span><span class="pln">listen</span><span class="pun">(</span><span
                class="lit">8888</span><span class="pun">);</span><span class="pln"><br>&nbsp; console</span><span
                class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">"Server has started."</span><span
                class="pun">);</span><span class="pln"><br></span><span class="pun">}</span><span class="pln"><br><br>exports</span><span
                class="pun">.</span><span class="pln">start </span><span class="pun">=</span><span
                class="pln"> start</span><span class="pun">;</span></pre>

        <p>
            And let's extend our <em>index.js</em> accordingly, that is,
            injecting the route function of our router into the server:
        </p>
        <pre class="prettyprint lang-js"><span class="kwd">var</span><span class="pln"> server </span><span class="pun">=</span><span
                class="pln"> require</span><span class="pun">(</span><span class="str">"./server"</span><span
                class="pun">);</span><span class="pln"><br></span><span class="kwd">var</span><span
                class="pln"> router </span><span class="pun">=</span><span class="pln"> require</span><span class="pun">(</span><span
                class="str">"./router"</span><span class="pun">);</span><span class="pln"><br><br>server</span><span
                class="pun">.</span><span class="pln">start</span><span class="pun">(</span><span
                class="pln">router</span><span class="pun">.</span><span class="pln">route</span><span
                class="pun">);</span><span class="pln"><br></span></pre>

        <p>
            Again, we are passing a function, which by now isn't any news
            for us.
        </p>

        <p>
            If we start our application now (<em>node index.js,
            as always</em>), and request an URL, you can now see from the
            application's output that our HTTP server makes use of our
            router and passes it the requested pathname:
        </p>
        <pre>bash$ node index.js
Request for /foo received.
About to route a request for /foo</pre>

        <p>
            (I omitted the rather annoying output for the /favicon.ico
            request).
        </p>

        <a name="execution-in-the-kongdom-of-verbs"></a>

        <h3>Execution in the kingdom of verbs</h3>

        <p>
            May I once again stray away for a while and talk about
            functional programming again?
        </p>

        <p>
            Passing functions is not only a technical consideration.
            With regard to software design, it's almost philosophical.
            Just think about it: in our index file, we could have passed
            the <em>router</em> object into the server, and the server
            could have called this object's <em>route</em> function.
        </p>

        <p>
            This way, we would have passed a <em>thing</em>, and the server
            would have used this thing to <em>do</em> something. Hey,
            router thing, could you please route this for me?
        </p>

        <p>
            But the server doesn't need the thing. It only needs to get
            something <em>done</em>, and to get something done, you don't
            need things at all, you need <em>actions</em>. You don't need
            <em>nouns</em>, you need <em>verbs</em>.
        </p>

        <p>
            Understanding the fundamental mind-shift that's at the core of
            this idea is what made me really understand functional
            programming.
        </p>

        <p>
            And I did understand it when reading Steve Yegge's masterpiece
            <a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html">Execution in the Kingdom of Nouns</a>.
            Go read it now, really. It's one of the best writings related
            to software I ever had the pleasure to encounter.
        </p>

        <a name="routing-to-real-request-handlers"></a>

        <h3>Routing to real request handlers</h3>

        <p>
            Back to business. Our HTTP server and our request router are
            now best friends and talk to each other as we intended.
        </p>

        <p>
            Of course, that's not enough. "Routing" means, we want to
            handle requests to different URLs differently. We would like to
            have the "business logic" for requests to <em>/start</em>
            handled in another function than requests to <em>/upload</em>.
        </p>

        <p>
            Right now, the routing "ends" in the router, and the router is
            not the place to actually "do" something with the requests,
            because that wouldn't scale well once our application becomes
            more complex.
        </p>

        <a name="continuereading"></a>
        <p>
            Let's call these functions, where requests are routed to,
            <em>request handlers</em>. And let's tackle those next, because
            unless we have these in place there isn't much sense in doing
            anything with the router for now.
        </p>
        </div>

        <div id="salespitch">
          <div id="bubble">
            <img id="authorimage" src="manuelkiessling.jpg" width="160" height="160"/>
            <p>
              <br/>
              Hi there! Sorry to interrupt you.
            </p>
            <p>
              My name is Manuel Kiessling, I'm the author of this book.
            </p>
            <p>
              If you have read that far, I would like you to consider buying the eBook version
              of this book. It's a beautifully crafted package including a PDF,
              an ePub, and a MOBI file, which means you can read it on all
              kinds of eReaders out there like the Amazon Kindle, the iPad, or
              the Sony Reader, and of course on any PC or Mac.
            </p>
            <p>
              But the best thing about it is that it comes bundled with another
              great Node.js book: <em>"Hands-on Node.js"</em> by Pedro Teixeira. It's a
              complete reference that explains all the Node.js modules in great
              detail - it's perfect to dig deeper once you've finished <em>The Node
              Beginner Book</em>.
            </p>
            <p>
              Both books together would cost a total of $21.98, but we are offering
              it as a bundle for only <strong>$9.99</strong>.
              You can download it immediately, it's
              completely DRM-free, and you will receive any future updates to
              both books for free.
            </p>
            <div class="box">
              <a class="buttonlink" href="buy-bundle/salespitch.html">
                <div class="button">Buy this<br />bundle now</div>
              </a>
              <p></p>
              <a href="buy-bundle/salespitch.html"><img src="the_node_beginner_book_cover_small.png" height="86" width="57" /></a>
              <a href="buy-bundle/salespitch.html"><img src="hands-on_node.js_cover.png" height="86" width="57" /></a>
              <br/>
              <div class="subinfo">
                226 pages in total<br/>
                DRM-free<br/>
                Free updates<br/>
                <strong>Only $9.99</strong>
              </div>
            </div>
          </div>
          <br clear="all"/>
        </div>

        </div>

        <div id="praise">
            <div class="praise">
                <div class="comment">
                    "This is an amazing introduction to Node."
                </div>
                <div class="author">
                    Ryan Dahl, creator of Node.js
                </div>
            </div>
            <div class="praise">
                <div class="comment">
                    "I love nodebeginner.org - concise, direct to the point and
                    even enjoyable to read."
                </div>
                <div class="author">Gojko Adzic, author of <em>Specification by Example</em> and <em>Bridging the Communication Gap</em></div>
            </div>
            <div class="praise">
                <div class="comment">
                    "This is one of the best tutorials I've read.
                    As a former Java coder, I've always found JavaScript
                    to be a black art, but you have really simplified
                    things with this tutorial."
                </div>
                <div class="author">Erskine, from the comments</div>
            </div>
            <div class="praise">
                <div class="comment">
                    "This is one of the few beginner articles I made it all the
                    way through because of how well it's written."
                </div>
                <div class="author">
                    Paul Gibler, from the comments
                </div>
            </div>
            <div class="praise">
                <div class="comment">
                    "Indispensable."
                </div>
                <div class="author">
                    @lecolibrilibre, on Twitter
                </div>
            </div>
            <div class="praise">
                <div class="comment">
                    "I just wanted to drop you a note to say thank you for
                    writing such an excellent introduction to node. Your book's
                    explanation is fantastic, and I can't wait for you to
                    finish it!"
                </div>
                <div class="author">
                    Seth McLaughlin, via eMail
                </div>
            </div>
        </div>

        <div id="disqus_thread"></div>

        <div id="footer">
            <p id="ccimage">
                <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/"><img alt="Creative Commons License" border="0" src="creative_commons.png" width="88" height="31"/></a>
            </p>
            <p>
                <span xmlns:dct="http://purl.org/dc/terms/">The Node Beginner Book</span>
                by
                <a xmlns:cc="http://creativecommons.org/ns#" href="http://manuel.kiessling.net" rel="cc:attributionURL">Manuel Kiessling</a> (see <a href="https://plus.google.com/100272082905360445612?rel=author">Google+ profile</a>)
                is licensed under a
                <br />
                <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
                <br />
                Permissions beyond the scope of this license may be available at <a xmlns:cc="http://creativecommons.org/ns#" href="mailto:manuel@kiessling.net" rel="cc:morePermissions">manuel@kiessling.net</a>.
            </p>
        </div>

        <script type="text/javascript">
            // Google Analytics
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', 'UA-2127388-6']);
            _gaq.push(['_trackPageview']);

            (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();

            // Disqus
            var disqus_shortname = 'nodebeginner';
            var disqus_identifier = 'nodebeginner-book';
            var disqus_url = 'http://www.nodebeginner.org/';
            (function() {
                var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
                (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();

            // CSS browser select
            function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js'];c=b.join(' ');h.className+=' '+c;return c;};css_browser_selector(navigator.userAgent);
        </script>
        <script></script>
    </body>
</html>
